#include<iostream>
#include<vector>

using namespace std;

int maxProduct(vector<int>& nums) {
    int n = nums.size();
    vector<long> fmax(nums.begin(), nums.end()), fmin(nums.begin(), nums.end());

    long res = fmax[0];
    for (int i = 1; i < n; i++)
    {
        fmax[i] = max(fmax[i - 1] * nums[i], max((long)nums[i], nums[i] * fmin[i - 1]));
        fmin[i] = min(fmin[i - 1] * nums[i], min((long)nums[i], nums[i] * fmax[i - 1]));
        res = max(res, fmax[i]);
    }
    return res;
}